package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.pojo.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 套餐管理
 * */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增套餐
     * */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("新增套餐信息为：{}",setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("添加成功");
    }

    /**
     * 套餐分页查询
     * */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //分页构造器
        Page<Setmeal> page1 = new Page<>(page,pageSize);
        Page<SetmealDto> page2 = new Page<>();
        //条件构造器
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name!=null,Setmeal::getName,name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(page1,queryWrapper);
        BeanUtils.copyProperties(page1,page2,"records");
        List<Setmeal> list = page1.getRecords();
        List<SetmealDto> list2 = new ArrayList<>();
        for (Setmeal setmeal : list) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            Category category = categoryService.getById(setmeal.getCategoryId());
            if (category != null) {
                setmealDto.setCategoryName(category.getName());
            }
            list2.add(setmealDto);
        }
        page2.setRecords(list2);
        return R.success(page2);
    }

    /**
     * 根据id查询套餐信息和对应的菜品信息
     * */
    @GetMapping("/{id}")
    public R<SetmealDto> get(@PathVariable Long id){
        log.info("查询的套餐id为:{}",id);
        SetmealDto setmealDto = setmealService.getByIdWithDish(id);

        return R.success(setmealDto);
    }

    /**
    * 修改套餐功能
    * */
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){

        setmealService.updateWithDish(setmealDto);
        return R.success("修改成功");
    }

    /**
     * 批量和单个启售停售套餐
     * */
    @Transactional
    @PostMapping("/status/{id}")
    public R<String> status(@PathVariable int id,String ids){
        log.info("修改的套餐id和状态是：{},{}",ids,id);
        String [] arr = ids.split(",");
        for (String s : arr) {
            Setmeal setmeal = setmealService.getById(s);
            if(setmeal.getStatus()==id){
                if(setmeal.getStatus()==0){
                    return R.error(setmeal.getName()+"菜品已是停售状态，无法修改");
                }else{
                    return R.error(setmeal.getName()+"菜品已是启售状态，无法修改");
                }
            }
        }
        for (String s : arr) {
            Setmeal setmeal = setmealService.getById(s);
            setmeal.setStatus(id);
            setmealService.updateById(setmeal);
        }
        return R.success("修改成功");
    }

    /**
     * 删除及批量删除套餐
     */
    @DeleteMapping
    public R<String> delete(String ids){
        log.info("删除的套餐id为：{}",ids);
        setmealService.deleteWithDish(ids);
        return R.success("删除成功");
    }

    /**
     * 根据条件查询套餐数据
     * */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(queryWrapper);

        return R.success(list);
    }



}
